<template>
  <full-dialog :visible.sync="visible" :fullscreen.sync="fullscreen" title="项目详细" width="80%" @closed="reset">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="执行状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择执行状态"
          clearable
          size="small"
          style="width: 200px">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="执行时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 200px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['monitor:job:remove']">
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"
          v-hasPermi="['monitor:job:remove']">
          清空
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="initData"></right-toolbar>
    </el-row>

    <el-table v-loading="table.loading" :data="table.data" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="任务名称" align="center" prop="jobName" :show-overflow-tooltip="true"/>
      <el-table-column label="任务组名" align="center" prop="jobGroup" :formatter="jobGroupFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="调用目标字符串" align="center" prop="invokeTarget" :show-overflow-tooltip="true"/>
      <el-table-column label="日志信息" align="center" prop="jobMessage" :show-overflow-tooltip="true"/>
      <el-table-column label="执行状态" align="center" prop="status">
        <template slot-scope="scope">
          <status-tag :status-options="statusOptions" :status="scope.row.status"></status-tag>
        </template>
      </el-table-column>
      <el-table-column label="执行时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-link
            type="primary"
            icon="el-icon-view"
            :underline="false"
            @click="handleView(scope.row)">
            详细
          </el-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="page.total>0"
      :total="page.total"
      :page.sync="page.current"
      :limit.sync="page.size"
      @pagination="initData"/>

    <log-detail ref="logDetail" :status-options="statusOptions" :job-group-options="jobGroupOptions"></log-detail>
  </full-dialog>
</template>

<script>
import {cleanJobLog, delJobLog, listJobLog} from "@/api/monitor/jobLog";
import LogDetail from "./logDetail";

export default {
  name: "JobLog",
  components: {
    LogDetail
  },
  props: {
    jobGroupOptions: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      jobId: undefined,
      visible: false,
      fullscreen: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 执行状态字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        status: undefined
      },
      table: {
        data: [],
        loading: false,
      },
      page: {
        total: 0,
        current: 1,
        size: 10,
      }
    }
  },
  methods: {
    init(jobId) {
      this.visible = true;
      this.jobId = jobId;
      this.initData();
      this.getDicts("sys_common_status").then(response => {
        this.statusOptions = response;
      });
    },
    /** 查询调度日志列表 */
    initData() {
      this.table.loading = true;
      listJobLog({
        jobId: this.jobId,
        status: this.queryParams.status,
        beginTime: this.dateRange[0],
        endTime: this.dateRange[1],
        page: true,
        current: this.page.current,
        size: this.page.size
      }).then(response => {
        this.table.data = response.records;
        this.page.total = response.total;
        this.table.loading = false;
      }).catch(() => {
        this.table.loading = false;
      });
    },
    // 任务组名字典翻译
    jobGroupFormat(row, column) {
      return this.selectDictLabel(this.jobGroupOptions, row.jobGroup);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.page.current = 1;
      this.initData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.multiple = !selection.length;
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.$refs.logDetail.init(row.id);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jobLogIds = row.id || this.ids;
      this.$confirm('是否确认删除调度日志?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delJobLog(jobLogIds);
      }).then(() => {
        this.handleQuery();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 清空按钮操作 */
    handleClean() {
      const jobId = this.jobId;
      this.$confirm("是否确认清空所有调度日志数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return cleanJobLog(jobId);
      }).then(() => {
        this.handleQuery();
        this.msgSuccess("清空成功");
      }).catch(() => {});
    },
    close() {
      this.visible = false;
    },
    // 表单重置
    reset() {
      this.jobId = undefined;
      // 选中数组
      this.ids = [];
      // 非多个禁用
      this.multiple = true;
      // 日期范围
      this.dateRange = [];
      // 查询参数
      this.queryParams = {
        status: undefined
      };
      this.table = {
        data: [],
        loading: false,
      };
      this.page = {
        total: 0,
        current: 1,
        size: 10,
      }
    }
  }
}
</script>

<style scoped>

</style>
